package com.winapi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.winapi.project.common.ErrorCode;
import com.winapi.project.exception.BusinessException;
import com.winapi.project.mapper.UserInterfaceInfoMapper;
import com.winapi.project.model.dto.userinterfaceinfo.UpdateUserInterfaceInfoDTO;
import com.winapi.project.model.vo.UserInterfaceInfoVO;
import com.winapi.project.service.UserInterfaceInfoService;
import com.winapi.winapicommon.model.entity.UserInterfaceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 张莹莹
* @description 针对表【user_interface_info(用户调用接口关系)】的数据库操作Service实现
* @createDate 2023-12-06 15:40:57
*/
@Service
@Slf4j
public class UserInterfaceInfoServiceImpl extends ServiceImpl<UserInterfaceInfoMapper, UserInterfaceInfo>
    implements UserInterfaceInfoService {
@Resource
private UserInterfaceInfoMapper userInterfaceInfoMapper;
    @Override
    public void validUserInterfaceInfo(UserInterfaceInfo userInterfaceInfo, boolean add) {
        log.info("用户接口业务层-判断用户接口信息是否合法-进入业务层....");
        if (userInterfaceInfo == null) {
            log.error("用户接口业务层-判断用户接口信息是否合法-用户接口这个对象不存在");
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (userInterfaceInfo.getId()<=0||userInterfaceInfo.getInterfaceInfoId()<=0) {
            log.error("用户接口业务层-判断用户接口信息是否合法-用户接口的id不合法");
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"接口或用户不存在");
        }
        if (userInterfaceInfo.getLeftNum()<0) {
            log.error("用户接口业务层-判断用户接口信息是否合法-该用户的这个接口的调用次数不足");
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口剩余调用次数小于0");
        }
        log.info("用户接口业务层-判断用户接口信息是否合法-结束业务层....");
    }

    @Override
    public boolean invokeCount(long interfaceInfoId, long userId) {
        log.info("用户接口业务层-调用次数减少1-进入业务层.....");
        //判断
        if (interfaceInfoId <=0 ||userId <=0) {
            log.error("用户接口业务层-调用次数减少1-进入业务层.....");
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
            UpdateWrapper<UserInterfaceInfo> updateWrapper =new UpdateWrapper<>();
            updateWrapper.eq("interfaceInfoId",interfaceInfoId);
            updateWrapper.eq("userId",userId);
            updateWrapper.setSql("leftNum =leftNum-1,totalNum=totalNum+1");
        log.info("用户接口业务层-调用次数减少1-结束业务层.....");
            return this.update(updateWrapper);
    }

    @Override
    public List<UserInterfaceInfo> topNumber() {
        log.info("用户接口业务层-调用次数前三的接口id和调用次数-进入业务层....");
        return userInterfaceInfoMapper.topNumber();
    }

    @Override
    public List<UserInterfaceInfoVO> getInterfaceInfoByUserId(Long userId) {
        log.info("用户接口业务层-根据用户id查询接口信息-进入业务层....");
        return userInterfaceInfoMapper.getInterfaceInfoByUserId(userId);
    }

    /**
     * 根据用户id和接口id来查询相关对象
     * @param userId
     * @param id
     * @return
     */
    @Override
    public UserInterfaceInfo getInterfaceInfoByUserIdAndInterfaceInfoId(Long userId, long id) {
        log.info("用户接口业务层-根据用户id和接口id来查询-进入业务层....");
        QueryWrapper<UserInterfaceInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("userId",userId);
        queryWrapper.eq("interfaceInfoId",id);
         UserInterfaceInfo userInterfaceInfo = userInterfaceInfoMapper.selectOne(queryWrapper);
        log.info("用户接口业务层-根据用户id和接口id来查询-在数据库中根据用户id和接口id查询");
        log.info("用户接口业务层-根据用户id和接口id来查询-结束业务层....");
        return userInterfaceInfo;

    }

    @Override
    public boolean updateUserInterfaceInfo(UpdateUserInterfaceInfoDTO updateUserInterfaceInfoDTO) {
        log.info("用户接口业务层-更新用户接口信息-进入业务层....");
        Long userId = updateUserInterfaceInfoDTO.getUserId();
        Long interfaceId = updateUserInterfaceInfoDTO.getInterfaceInfoId();
        Long lockNum = updateUserInterfaceInfoDTO.getLockNum();

        if (interfaceId == null || userId == null || lockNum == null) {
            log.error("用户接口业务层-更新用户接口信息-接口id/用户id/购买、获取次数为空");
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        UserInterfaceInfo one = this.getOne(
                new QueryWrapper<UserInterfaceInfo>()
                        .eq("userId", userId)
                        .eq("interfaceInfoId", interfaceId)
        );
        log.info("用户接口业务层-更新用户接口信息-在数据库中根据用户id和接口id查询记录");
        if (one != null) {
            log.info("用户接口业务层-更新用户接口信息-记录存在，更新用户剩余调用次数");
            log.info("用户接口业务层-更新用户接口信息-结束业务层....");
            // 说明是增加数量
            return this.update(
                    new UpdateWrapper<UserInterfaceInfo>()
                            .eq("userId", userId)
                            .eq("interfaceInfoId", interfaceId)
                            .setSql("leftNum = leftNum + " + lockNum)
            );
        } else {
            // 说明是第一次购买
            log.info("用户接口业务层-更新用户接口信息-第一次购买/获取接口");
            UserInterfaceInfo userInterfaceInfo = new UserInterfaceInfo();
            userInterfaceInfo.setUserId(userId);
            userInterfaceInfo.setInterfaceInfoId(interfaceId);
            userInterfaceInfo.setLeftNum(Math.toIntExact(lockNum));
            log.info("用户接口业务层-更新用户接口信息-插入记录");
            log.info("用户接口业务层-更新用户接口信息-结束业务层....");
            return this.save(userInterfaceInfo);
        }
    }
}




